package com.maoye.ossp.biz.brand.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.entity.SupplierBank;
import com.maoye.ossp.biz.brand.entity.SupplierRegister;
import com.maoye.ossp.biz.brand.service.ISupplierBankService;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:供应商品牌关系 控制器类 开发公司:maoye shenzhen 开发人员:lyf 创建时间:2013-09-30 10:04:52
 */
@Controller
@RequestMapping("/biz/brand/supplierBank/")
public class SupplierBankController extends BaseController {
	@Resource
	private ISupplierBankService supplierBankService;
	@Resource
	private ISupplierService supplierService;

	/**
	 * 取得供应商银行关系信息分页列表
	 * 
	 * @param supplier
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("list")
	public String list(Supplier supplier, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		List<Supplier> list = supplierService.getAll();
		model.addAttribute("supplierList", list);
		return resultPage("/biz/brand/supplierBank/list", model, request);
	}

	/**
	 * 添加或更新供应商银行信息。
	 * 
	 * @param supplier添加或更新的实体
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(SupplierBank supplierBank, String delBankId, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) {
		try {
			// 添加或者修改银行信息
			supplierBankService.save(supplierBank, delBankId);
			//addMessage(model,message);
		} catch (Exception e) 
		{
			addMessage(model, "操作失败");
		}
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		setSupplierInfo(supplierBank, model);
		return "/biz/brand/supplier/apply";
	}
	
	@RequestMapping(value = "add")
	public String add(SupplierBank supplierBank, Model model,HttpServletRequest request)
	{
		//supplierBank = supplierBankService.getById(null);
		model.addAttribute("supplierBank", supplierBank);
		return resultPage("/biz/brand/supplierBank/add", model, request) ;
	}

	/**
	 * 编辑供应商银行信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		SupplierBank supplierBank = supplierBankService.getById(null);
		model.addAttribute("supplierBank", supplierBank);
		return "/biz/brand/supplierBank/add";
	}

	/**
	 * 删除供应商子账号信息
	 * 
	 * @param id
	 *            子账号id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
		try {
			// 删除供应商银行信息
			supplierBankService.delByMainId(Long.valueOf(id));
			addMessage(redirectAttributes, "删除数据成功！");
		} catch (NumberFormatException e) {
			addMessage(redirectAttributes, "删除数据失败！");
			e.printStackTrace();
		}
		return "redirect:/biz/brand/supplierBank/list";
	}
	
	private void setSupplierInfo(SupplierBank supplierBank, Model model)
	{
		Supplier supplier = supplierService.get(supplierBank.getSupplierId());
		if(supplier == null)
		{
			supplier = new Supplier();	
		}
		if(null == supplier.getSupplierRegister())
		{
			supplier.setSupplierRegister(new SupplierRegister());
			supplier.getSupplierRegister().setSupplierAccountId(supplier.getId());
		}
		model.addAttribute("supplier",supplier);
		model.addAttribute("supplierRegister",supplier.getSupplierRegister());
		model.addAttribute("tabId", "att");
	}
}
